from rest_framework import serializers
from books.models import BookInfo,HeroInfo


""" 第一种和第二种方法 """
# 创建书本详情路由
class BookInfoSerializers(serializers.ModelSerializer):
    class Meta:
        model = BookInfo
        fields = ['title'] # 定义所需要的字段
# 创建英雄序列化器
class HeroInfoSerializer(serializers.ModelSerializer):
    # 嵌套书本的序列化器
    bookid = BookInfoSerializers()  # bookid 就是模型中声明的外键字段，非外键字段不能指定序列化器选项

    class Meta:
        model = HeroInfo
        fields = '__all__'


""" 第三种办法 """
class HeroInfoSerializer2(serializers.ModelSerializer):
    # 嵌套书本的序列化器
    book_name = serializers.CharField(source="bookid.title")

    class Meta:
        model = HeroInfo
        fields = ["id", "book_name", 'name']


""" 第四种方法 """
class HeroInfoSerializer3(serializers.ModelSerializer):

    class Meta:
        model = HeroInfo
        fields = "__all__"
        depth = 1


""" 第五种方法 """
class HeroInfoSerializer4(serializers.ModelSerializer):

    class Meta:
        model = HeroInfo
        fields = ["id",'bookName']

